//
//  ViewController.m
//  3 - 22作业 image
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong)NSArray *arr;

@property (nonatomic,strong)NSMutableArray *mArrView;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];

}
- (IBAction)btnPressed:(id)sender
{
    [self.activityView startAnimating];
    int colCount = 3;
    for (int i=0;i<self.arr.count;i++)
    {
        //设置行和列
        int a=i/colCount;
        int b=i%colCount;
        //设置图片大小
        int width = 100;
        int height = 100;
        
        int x = width*b + 20;
        int y = height*a + 100;
        
        [self addImageWithImageInfo:self.arr[i] andX:x andY:y];
    }
}

-(void)addImageWithImageInfo:(NSString *)str andX:(int)x andY:(int)y
{
    [self.activityView startAnimating];
    
    //定义一个组
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    
    //将dispatch_async放入到组里 执行 将要做的任务
    dispatch_async(queue, ^{
        
        UIImage *image = [[UIImage alloc] init];
        image = [self addImageWithStr:str];
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        imgView.frame = CGRectMake(x, y, 100, 100);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.view addSubview:imgView];
            [self.activityView stopAnimating];
        });
    });
}
// 获取图片的方法

- (UIImage *)addImageWithStr:(NSString *)str
{
    //网址对象
    NSURL *url = [NSURL URLWithString:str];
    
    //获取数据
    NSData *data = [NSData dataWithContentsOfFile:url];
    
    //图像对象
    UIImage *image = [UIImage imageWithData:data];
    
    return image;
                    
                    }











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
